#!/bin/sh

WORKDIR="/usr/local/ircd/auth/"
URL='http://lines.dalnet.ru/auth.tgz'

OPENSSL='/usr/bin/openssl'
CUT='/usr/bin/cut'
LS='/bin/ls'
GREP='/usr/bin/grep'
TAR='/usr/bin/tar'
WGET='/usr/local/bin/wget'
#WGET='/usr/bin/fetch'
CP='/bin/cp'
CAT='/bin/cat'
KILL='/bin/kill'

MD5_FILE='auto.md5'
AUTH_MASK='auto*conf'
TAR_FILE='auth.tgz'
TARGET_DIR='/usr/local/ircd/etc/'
SIGNAL='HUP'
PID_FILE='/usr/local/ircd/etc/ircd.pid'

WGET_OPTIONS='-N -q -nd -np '
#WGET_OPTIONS=''
OPENSSL_OPTIONS='md5'
TAR_OPTIONS='zxf'

cd ${WORKDIR}
${WGET} ${WGET_OPTIONS} -P ${WORKDIR} ${URL}
if [ ${?} != 0 ]; then
	echo "ERROR: unable to get new files."; exit 1
	fi
${TAR} ${TAR_OPTIONS} ${TAR_FILE}
if [ ${?} != 0 ]; then
	echo "ERROR: unable to extract files from archive."; exit 1
	fi
for i in `${LS} ${AUTH_MASK}`; do
	rsum=`${OPENSSL} ${OPENSSL_OPTIONS} ${i} | ${CUT} -d" " -f2`
	csum=`${GREP} ${i} ${MD5_FILE} | ${CUT} -d" " -f1`
	if [ ${rsum} != ${csum} ]; then 
		echo "ERROR: checksum mismatch for ${i}: ${rsum} != ${csum}!"; exit 1
		fi
	done
${CP} ${AUTH_MASK} ${TARGET_DIR}
if [ ${?} != 0 ]; then
	echo "ERROR: unable to copy files to ${TARGET_DIR}."; exit 1
	fi
PID=`${CAT} ${PID_FILE}`
${KILL} -${SIGNAL} ${PID}
if [ ${?} != 0 ]; then
	echo "ERROR: unable to send ${SIGNAL} to ${PID}."; exit 1
	fi
exit 0
